<?php
/**
 * 个人信息
 *
 * @author      jinzhanyou
 * @version     1.0
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\ProfileUpdateRequest;
use App\Http\Requests\ProfileUpdatePasswordRequest;
use App\Models\Admin;
use Illuminate\Support\Facades\Auth;
class ProfileController extends Controller
{
    /**
     * 个人资料编辑页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        return view('admin.profile.edit')->with('admin', \Auth::user());
    }

    /**
     * 个人资料更新
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function update(ProfileUpdateRequest $request)
    {
        $data = $request->except(['role', 'username']);
        $id = $request->get('id');
        if (!$id) {
            return redirect()->to(route('admin.profile'))->withErrors('更新失败');
        }
        $admin = Admin::findOrFail($id);
        if ($admin->update($data)) {
            return redirect()->to(route('admin.profile'))->with(['status' => '更新用户成功']);
        }
        return redirect()->to(route('admin.profile'))->withErrors('系统错误');
    }

    /**
     * 个人密码修改页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function updatePasswordIndex()
    {
        return view('admin.profile.update_password')->with('admin', \Auth::user());
    }

    /**
     * 更新密码
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function updatePassword(ProfileUpdatePasswordRequest $request)
    {
        $data = $request->except(['role', 'username', 'password']);
        $id = $request->get('id');
        if (!$id) {
            return redirect()->to(route('admin.profile'))->withErrors('更新失败');
        }
        $admin = Admin::findOrFail($id);
        $data['password'] = bcrypt($request->get('password'));
        if ($admin->update($data)) {
            return redirect()->to(route('admin.profile.update.password.index'))->with(['status' => '更新密码成功']);
        }
        return redirect()->to(route('admin.profile.update.password.index'))->withErrors('系统错误');
    }

    /*
    * 控制台
    */
    public function control(){
        $adminData = \Auth::user();
        $groupData = $adminData->getRoleNames();
        $adminData['group_name'] = $groupData[0];
        $data = [];
        return view('admin.profile.control')->with('data',$data)->with('adminData',$adminData);
    }
}